﻿using System;
using System.Windows.Forms;

namespace ZYB_S01_音视频转换器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listbox_files.AllowDrop = true;
            listbox_files.DragDrop += Lb_files_DragDrop;
            listbox_files.DragEnter += Lb_files_DragEnter;
            SOFT_VER.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }
        private void btn_mp3_Click(object sender, EventArgs e)
        {
            //ffmpeg -i input.mp4 -vn -acodec libmp3lame -ar 44100 -ac 2 -ab 192k output.mp3   
            foreach (var item in listbox_files.Items)
            {
                //会一个一个的进行转换 comboBox1.Text="98k"
                Exec("ffmpeg -i \"" + item.ToString() + "\" -vn -acodec libmp3lame -ar 44100 -ac 2 -ab " + comboBox1.Text + " \"" + System.IO.Path.GetFileNameWithoutExtension(item.ToString()) + ".mp3\"");
                //以下命令所有的转换会同时进行
                // System.Diagnostics.Process.Start("ffmpeg","-i \"" + item.ToString() + "\" -vn -acodec libmp3lame -ar 44100 -ac 2 -ab " + comboBox1.Text + " \"" + System.IO.Path.GetFileName(item.ToString()) + ".mp3\"");
                textBox1.Text += item.ToString() + "转换完成\r\n";
            }
            listbox_files.Items.Clear();
        }
        private void btn_mpg_Click(object sender, EventArgs e)
        {
            /*
             ffmpeg -i input.mp4 \
              -target ntsc-dvd \  # 针对NTSC制式DVD（美日等地区，帧率29.97）
              # 或 -target pal-dvd  # 针对PAL制式DVD（中欧美等地区，帧率25）
              -qscale:v 2 \       # 视频质量（1-31，值越小质量越高，2-5推荐）
              -ac 2 \             # 音频声道（DVD通常支持2声道或5.1声道）
              -ar 48000 \         # 音频采样率（DVD标准48kHz）
              output.mpg
             */

            //ffmpeg -i input.mp4 -target ntsc-dvd -qscale:v 2 -ac 2 -b:a 128k output_128k.mpg
            foreach (var item in listbox_files.Items)
            {
                //会一个一个的进行转换 comboBox1.Text="98k"
                // string t = "ffmpeg -i \"" + item.ToString() + "\" -target ntsc-dvd -qscale:v 2 -ac 2 -b:a 128k  \"" + System.IO.Path.GetFileNameWithoutExtension(item.ToString()) + ".mpg\"";
                //textBox1.Text += t;
                //return;

                string 制式 = "pal-dvd";
                if (rbNTSC.Checked) 制式 = "ntsc-dvd";
                Exec("ffmpeg -i \"" + item.ToString() + "\" -target "+ 制式 + " -qscale:v 2 -ac 2 -b:a "+ cb_dvd音频码率.Text + "  \"" 
                    + System.IO.Path.GetDirectoryName(item.ToString()) +"\\"+ System.IO.Path.GetFileNameWithoutExtension(item.ToString()) + ".mpg\"");
                //以下命令所有的转换会同时进行
                // System.Diagnostics.Process.Start("ffmpeg","-i \"" + item.ToString() + "\" -vn -acodec libmp3lame -ar 44100 -ac 2 -ab " + comboBox1.Text + " \"" + System.IO.Path.GetFileName(item.ToString()) + ".mp3\"");
                textBox1.Text += item.ToString() + "转换完成\r\n";
            }
            listbox_files.Items.Clear();
        }
        private void Lb_files_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }

        private void Lb_files_DragDrop(object sender, DragEventArgs e)
        {
            foreach (string path in (System.Array)e.Data.GetData(DataFormats.FileDrop))
            {
                if (System.IO.Directory.Exists(path))
                {
                    string[] files = System.IO.Directory.GetFiles(path);
                    foreach (var item in files)
                    {
                        listbox_files.Items.Add(item.ToString());
                    }
                }
                else
                    listbox_files.Items.Add(path);
            }
            
        }

        public void Exec(string str)
        {
            try
            {
                using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                {
                    process.StartInfo.FileName = "cmd.exe";//调用cmd.exe程序
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;//重定向标准输入
                    process.StartInfo.RedirectStandardOutput = false;//重定向标准输出
                    process.StartInfo.RedirectStandardError = false;//重定向标准出错
                    if (cb显示ffmpeg转换界面.Checked) process.StartInfo.CreateNoWindow = false;
                   else  process.StartInfo.CreateNoWindow = true;//不显示黑窗口
                    process.Start();//开始调用执行
                    process.StandardInput.WriteLine(str + "&exit");//标准输入str + "&exit"，相等于在cmd黑窗口输入str + "&exit"
                    process.StandardInput.AutoFlush = true;//刷新缓冲流，执行缓冲区的命令，相当于输入命令之后回车执行
                    process.WaitForExit();//等待退出
                    process.Close();//关闭进程
                }
            }
            catch (Exception ex)
            {
                textBox1.Text += ex.Message;
            }
        }

        private void Process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            textBox1.Text += e.Data.ToString() + "\r\n";
        }

        //执行关机操作
        public void Shutdown()
        {
            this.Exec("shutdown -s -f -t 0");
        }
        //执行重启操作
        public void Restart()
        {
            this.Exec("shutdown -r -f -t 0");
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://zyb.pub");
        }

        private void listbox_files_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}
